package com.acmen.easyapi.validation;

import com.acmen.easyapi.util.SpringContextUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 获取自定义校验器说明
 *
 * @author acmen
 */
@Component
public class ValidatorNoteAppender {

    public List<String> getAllNotes() {
        List<String> notes = new ArrayList<>();
        Map<String, DataTypeValidator> dataValidatorMap = SpringContextUtil.getBeansOfType(DataTypeValidator.class);
        for (Map.Entry<String, DataTypeValidator> entry : dataValidatorMap.entrySet()) {
            DataTypeValidator validator = entry.getValue();
            notes.add(validator.getNote(null));
        }
        return notes;
    }

    public String getNote(String exp) {
        Map<String, DataTypeValidator> dataValidatorMap = SpringContextUtil.getBeansOfType(DataTypeValidator.class);
        for (Map.Entry<String, DataTypeValidator> entry : dataValidatorMap.entrySet()) {
            DataTypeValidator validator = entry.getValue();
            if (validator.matches(exp)) {
                return validator.getNote(exp);
            }
        }
        return null;
    }
}
